package com.webtest.demo;
import java.time.Duration;
import java.util.Set;

import org.asynchttpclient.filter.ThrottleRequestFilter;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

import com.webtest.core.BaseTest;
import com.webtest.dataprovider.ExcelDataProvider;
import com.webtest.dataprovider.TxtDataProvider;

public class Demo extends BaseTest {

	String front_url = "http://localhost/MetInfo7.1.0/";
	String admin_url = "http://localhost/MetInfo7.1.0/admin/";
	WebDriver driver ;
	
	Actions action = new Actions(driver);
	
	JavascriptExecutor js = (JavascriptExecutor)driver;
	
	String old_handle = driver.getWindowHandle();
	
	@Test(priority=0)
	public void Front_Login(String url) throws Exception {
		
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
		
		driver.manage().window().maximize();
		
		driver.get(front_url);
		Thread.sleep(3000);
		
		driver.findElement(By.linkText("登录")).click();

		driver.findElement(By.name("username")).sendKeys("huangxinyue");
		Thread.sleep(2000);

		driver.findElement(By.name("password")).sendKeys("123456");
		Thread.sleep(2000);

		driver.findElement(By.linkText("登录")).click();
		Thread.sleep(2000);
	}
	
	@Test(priority=0)
	public void Admin_Login(String url) throws Exception{
		
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
		
		driver.manage().window().maximize();
		
		driver.get(admin_url);
		Thread.sleep(3000);

		driver.findElement(By.name("login_name")).sendKeys("hxy");
		Thread.sleep(2000);

		driver.findElement(By.name("login_pass")).sendKeys("123456");
		Thread.sleep(2000);

		driver.findElement(By.linkText("登录")).click();
		Thread.sleep(2000);
	}
	
	@Test(dataProvider="a",dataProviderClass=TxtDataProvider.class)
	public void test1() throws Exception{
		//对登陆的测试 用户名密码为空，用户名不存在，密码不正确
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
		
		driver.manage().window().maximize();
		
		driver.get(front_url);
		Thread.sleep(3000);
		
		driver.findElement(By.linkText("登录")).click();

		driver.findElement(By.name("username")).sendKeys("huangxinyue");
		Thread.sleep(2000);

		driver.findElement(By.name("password")).sendKeys("123456");
		Thread.sleep(2000);

		driver.findElement(By.linkText("登录")).click();
		Thread.sleep(2000);
	}
	
	@Test(dataProvider="register",dataProviderClass=ExcelDataProvider.class)
	public void test2(String username,String password,String confirmpassword) throws Exception {
		//对注册时的用户名、密码、重复密码的验证
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
		driver.manage().window().maximize();
		driver.get(front_url);
		Thread.sleep(3000);
		
		driver.findElement(By.linkText("注册")).click();
		Thread.sleep(1000);
		
		driver.findElement(By.name(username)).sendKeys(username);
		Thread.sleep(1000);
		driver.findElement(By.name(password)).sendKeys(password);
		Thread.sleep(1000);
		driver.findElement(By.name(confirmpassword)).sendKeys(confirmpassword);
		Thread.sleep(1000);
		
		driver.findElement(By.linkText("立即注册")).click();
		Thread.sleep(1000);
		
		
	}
	
	@Test
	public void test3() throws Exception {
		//上传文件修改头像
		Front_Login(front_url);
		this.driver.manage().window().maximize();
		
		driver.findElement(By.linkText("huangxinyue")).click();
		Thread.sleep(1000);
		driver.findElement(By.linkText("个人信息")).click();
		Thread.sleep(1000);
		
		driver.findElement(By.className("btn btn-primary btn-file m-b-0")).sendKeys("C:\\Users\\19646\\Pictures\\Saved Pictures\\月亮.jpg");
		Thread.sleep(1000);
		driver.switchTo().frame(0);
		Thread.sleep(1000);
		driver.findElement(By.linkText("保存资料")).click();
		Thread.sleep(1000);
	}
	

	
	@Test
	public void test4() throws Exception {
		//单选 少写一个星号空白
		Front_Login(front_url);
		this.driver.manage().window().maximize();
		
		driver.findElement(By.linkText("加入我们")).click();
		Thread.sleep(1000);
		driver.findElement(By.linkText("在线应聘")).click();
		Thread.sleep(1000);
		
		driver.findElement(By.name("para163")).sendKeys("lala");
		Thread.sleep(1000);
		driver.findElement(By.id("para164_2")).click();
		Thread.sleep(1000);
		driver.findElement(By.name("para167")).sendKeys("1111111111");
		Thread.sleep(1000);
		
		driver.findElement(By.linkText("提交信息")).click();
		
	}
	
	@Test
	public void test5() throws Exception {
		//回车进行搜索岗位
		Front_Login(front_url);
		driver.manage().window().maximize();
		
		driver.findElement(By.linkText("加入我们")).click();
		Thread.sleep(1000);
		driver.findElement(By.id("0")).sendKeys("市场");
		//回车
		action.sendKeys(Keys.ENTER).perform();
		Thread.sleep(1000);
//		action.sendKeys(Keys.DOWN).perform();
//		action.sendKeys(Keys.ARROW_DOWN).perform();
		
	}
	
	@Test
	public void test6() throws Exception {
		//双击后是否打开新的一个页面
		Front_Login(front_url);
		driver.manage().window().maximize();
		Thread.sleep(1000);
		
		WebElement h1 = driver.findElement(By.linkText("加入我们"));
		action.doubleClick(h1).perform();
		Thread.sleep(1000);
		
		driver.get("http://baidu.com");
		Thread.sleep(1000);
		
	}
	
	@Test
	public void test7() throws Exception{
		//对登录的测试
		driver.get(front_url);
		driver.manage().window().maximize();
		Thread.sleep(1000);
		driver.findElement(By.linkText("登录")).click();
		Thread.sleep(1000);
		
		WebElement username = driver.findElement(By.name("username"));
		action.sendKeys(username,"huangxinyue").perform();
		Thread.sleep(1000);
		
		action.sendKeys(Keys.TAB).perform();
		action.sendKeys("123456");
		Thread.sleep(1000);
		
		driver.findElement(By.linkText("登录")).click();
		Thread.sleep(1000);
	}
	
	@Test
	public void test8() throws Exception{
		//前端转换中英文
		Front_Login(front_url);
		driver.manage().window().maximize();
		Thread.sleep(1000);
		
		//使用滚动条到页面最下面
		String title = (String) js.executeScript("retrun document,title");
		System.out.println(title);
		
		js.executeScript("window.scroll(0,doucment.body.scrollHeight)");
		driver.navigate().refresh();
		js.executeScript("arguments[0].scrollIntoView",
				driver.findElement(By.linkText("huangxinyue")));
		driver.navigate().refresh();
		js.executeScript("window.scrollBy(10,500)");
		Thread.sleep(3000);
		js.executeScript("return window.title");
		
//		单选
		WebElement h1 = driver.findElement(By.linkText("简体中文"));
		Select s1 = new Select(h1);
		s1.selectByIndex(1);//2，从0开始计数
		System.out.println(s1.isMultiple());
		Thread.sleep(1000);
		
	}
	

	
	@Test
	public void test9() throws Exception{
		//从可视化编辑页面跳转到传统后台
		//元素定位不到，窗口页面切换
		Admin_Login(admin_url);
		driver.manage().window().maximize();
		Thread.sleep(1000);
		
		driver.findElement(By.linkText("传统后台")).click();
		Thread.sleep(1000);
		
		//报错，多窗口问题需要切换窗口
//		driver.findElement(By.linkText("不再提示")).click();
		
		//打印title，查看当前焦点位置
		System.out.println(driver.getTitle());
		//切换窗口
		System.out.println(driver.getWindowHandle());
		//获取当前窗口
		String old_handle = driver.getWindowHandle();
		Set<String> windows = driver.getWindowHandles();
		for(String window:windows) {
			Thread.sleep(1000);
			driver.switchTo().window(window);
			String current_title = driver.getTitle();
			if (current_title.contains("后台首页")) {
				driver.findElement(By.linkText("不再提示")).click();
				
			}
		}
		//窗口切换回
		driver.switchTo().window(old_handle);
		Thread.sleep(1000);
		driver.quit();
		
	}
	
	
}
